package com.itheima.sc.impl.ums;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.ums.UmsMenuApi;
import com.itheima.sc.mapper.ums.UmsMenuMapper;
import com.itheima.sc.pojo.ums.UmsMenu;
import com.itheima.sc.vo.PageResult;
import com.itheima.sc.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsMenuServiceImpl implements UmsMenuApi {
    @Autowired
    private UmsMenuMapper umsMenuMapper;

    /**
     * 添加后台菜单
     */
    @Override
    public void create(UmsMenu umsMenu) {
        umsMenuMapper.insert(umsMenu);
    }

    /**
     * 根据ID删除后台菜单
     */
    @Override
    public void delete(Integer id) {
        umsMenuMapper.deleteById(id);
    }

    /**
     * 分页查询后台菜单
     */
    @Override
    public PageResult selectPage(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);
        //构建查询条件
        LambdaQueryWrapper<UmsMenu> query = new LambdaQueryWrapper<>();
        query.eq(UmsMenu::getParentId, parentId);
        query.orderByDesc(UmsMenu::getCreateTime);
        umsMenuMapper.selectPage(page, query);

        //封装分页数据
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());

        //返回结果
        return pageResult;
    }

    /**
     * 查询所有父id为0的数据行
     */
    @Override
    public List<UmsMenu> getParentId() {
        //查询条件
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId, 0);
        List<UmsMenu> umsMenuList = umsMenuMapper.selectList(wrapper);
        return umsMenuList;
    }

    /**
     * 根据父id查询子id集合
     */
    @Override
    public List<UmsMenu> getChildren(Long id) {
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId, id);
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(wrapper);
        return umsMenus;
    }

    /**
     * 调用API修改菜单显示状态
     */
    @Override
    public void updateHidden(Integer id, Integer hidden) {
        umsMenuMapper.updateHidden(id, hidden);
    }

    /**
     * 根据ID获取菜单详情
     */
    @Override
    public UmsMenu getMenuInfo(Integer id) {
        return umsMenuMapper.selectById(id);
    }


    //获取所有的菜单数据
    public List<UmsMenu> getAll() {
        return umsMenuMapper.selectList(null);
    }

    /**
     * 修改后台菜单
     */
    @Override
    public void update(UmsMenu umsMenu) {
        umsMenuMapper.updateById(umsMenu);
    }

    /**
     * 根据 menuIds 查询 menus
     */
    @Override
    public List<UmsMenu> getByIds(List<Long> menuIds) {
        return umsMenuMapper.selectBatchIds(menuIds);
    }
}
